local ma__qianli = fk.CreateSkill {
  name = "ma__qianli",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["ma__qianli"] = "谦砺",
  [":ma__qianli"] = "锁定技，当每回合首次你的牌因弃置而进入弃牌堆时，你获得之，这些牌不能被弃置且使用时额外结算一次。",

  ["@@ma__qianli-inhand"] = "谦砺",

}

ma__qianli:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ma__qianli.name) and
      (data.extra_data or {}).usingQianli and ((data.card.type == Card.TypeBasic and data.card.trueName ~= "jink") or (data.card:isCommonTrick() and data.card.trueName ~= "nullification"))
  end,
  on_use = function(self, event, target, player, data)
    --if (data.card.type == Card.TypeBasic and data.card.trueName ~= "jink") or (data.card:isCommonTrick() and data.card.trueName ~= "nullification") then
      data.additionalEffect = (data.additionalEffect or 0) + 1
    --else player:drawCards(1, ma__qianli.name)
    --end
  end,
})

ma__qianli:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(ma__qianli.name) then return false end
    local cards = {}
    local mark = player:getTableMark("ma__qianli_record")
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard and move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and not table.contains(mark, info.cardId) then
            table.insert(cards, info.cardId)
          end
        end
      end
    end
    if #cards > 0 then
      event:setCostData(self, {extra_data = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).extra_data
    local dat = table.simpleClone(cards)
    --local mark = player:getTableMark("zhimin_record")
    if #dat > 0 then
      --table.insertTableIfNeed(mark, dat)
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, ma__qianli.name,
        nil, true, player, "@@ma__qianli-inhand")
    end
    --room:setPlayerMark(player, "ma__qianli_record", mark)
  end,
})

ma__qianli:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(ma__qianli.name, true) and data.card:getMark("@@ma__qianli-inhand") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.usingQianli = true
  end,
})

ma__qianli:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player.dead then return end
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@ma__qianli-inhand") > 0 then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local ids = {}
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard then
        local move_info = {}
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@ma__qianli-inhand") > 0 then
            table.insert(ids, info.cardId)
          else
            table.insert(move_info, info)
          end
        end
        if #ids > 0 then
          move.moveInfo = move_info
        end
      end
    end
  end,
})

ma__qianli:addEffect("prohibit", {
  prohibit_discard = function(self, player, card)
    return card and card:getMark("@@ma__qianli-inhand") > 0
  end,
})

return ma__qianli
